<template>
  <div>
    <div style="margin-top: 2%;text-align:center;">
      <el-select  v-model="form.academyId"
                  style="width: 16%;margin-right: 1%"
                  clearable filterable
                  placeholder="请选择学院">
        <el-option v-for="item in academy" :key="item.academyId"
                   :label="item.academyId +' - '+ item.academyName"
                   :value="item.academyId">
        </el-option>
      </el-select>
      <el-input style="width: 24%;margin-right: 1%" v-model="form.teacherName"
                placeholder="请输入待查找的教师ID/姓名/身份证/手机号(可指定学院)"></el-input>
      <el-button @click="findTeacher()" style="font-size: 20px" type="primary">立即查找</el-button>
      <el-button @click="addTeacher()" style="font-size: 20px" type="primary">点击此处新增教师</el-button>
    </div>

    <template style="margin-top: 2%">
      <el-table :data="pageInfo.list" style="width: 100%">
        <el-table-column label="" width="20">
        </el-table-column>
        <el-table-column label="教师编号" width="180" prop="teacherId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.teacherId}}</span>
          </template>
        </el-table-column>
        <el-table-column label="教师姓名" width="300" prop="teacherName" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.teacherName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="性别" width="100" prop="teacherSex" sortable>
          <template slot-scope="scope">
            <span v-if="scope.row.teacherSex===1">男</span>
            <span v-if="scope.row.teacherSex===2">女</span>
            <span v-if="scope.row.teacherSex===3">不明</span>
          </template>
        </el-table-column>
        <el-table-column label="身份证号"  width="320" prop="teacherIdCard" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.teacherIdCard}}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机" width="200" prop="teacherPhone" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.teacherPhone}}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属学院" width="360" prop="academyId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.academyName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button  size="mini" type="primary" style="font-size: 16px"
                        @click="handleEdit(scope.row)">编辑</el-button>
            <el-button  size="mini" type="warning" style="font-size: 16px"
                        @click="initPwd(scope.row)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-bottom: 1%;"></div>
      <el-pagination
          style="text-align: center;"
          background
          layout="total,prev, pager, next"
          :total="pageInfo.total"
          :page-size="pageInfo.pageSize"
          @current-change="handleCurrentChange">
      </el-pagination>
    </template>

    <el-dialog title="新增/编辑教师" :visible.sync="dialogForm" :before-close="handleClose">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="20%">
        <el-form-item  label="请输入教师编号" prop="teacherId">
          <el-input style="width: 40%;"
                    placeholder="请输入目标教师编号"
                    v-model="ruleForm.teacherId"
                    oninput ="value=value.replace(/[^0-9]/g,'')"></el-input>
        </el-form-item>
        <el-form-item  label="请输入教师姓名" prop="teacherName">
          <el-input style="width: 60%;" v-model="ruleForm.teacherName"
                    placeholder="请输入教师姓名"></el-input>
        </el-form-item>
        <el-form-item label="请选择性别" prop="teacherSex">
          <el-select v-model="ruleForm.teacherSex" style="width: 20%" placeholder="请选择性别">
            <el-option label="男" :value="1"/>
            <el-option label="女" :value="2"/>
            <el-option label="未知" :value="3"/>
          </el-select>
        </el-form-item>
        <el-form-item  label="请输入身份证号" prop="teacherIdCard">
          <el-input style="width: 40%;"
                    placeholder="请输入身份证号"
                    v-model="ruleForm.teacherIdCard"
                    oninput ="value=value.replace(/[^0-9Xx]/g,'')"></el-input>
        </el-form-item>
        <el-form-item  label="请输入手机号" prop="teacherPhone">
          <el-input style="width: 40%;"
                    placeholder="请输入手机号"
                    v-model="ruleForm.teacherPhone"
                    oninput ="value=value.replace(/[^0-9]/g,'')"></el-input>
        </el-form-item>
        <el-form-item  label="请选择所属学院" prop="academyId">
          <el-select  v-model="ruleForm.academyId"
                      style="width: 40%;"
                      clearable filterable
                      placeholder="请选择学院">
            <el-option v-for="item in academy" :key="item.academyId"
                       :label="item.academyId +' - '+ item.academyName"
                       :value="item.academyId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return {
      dialogForm: false,
      form:{
        teacherName: '',
        academyId: '',
      },
      academy:[],
      currPage: 1,
      pageInfo:{},
      ruleForm:{
        oldTeacherId:'',
        teacherId: '',
        teacherName: '',
        teacherSex: '',
        teacherIdCard: '',
        teacherPhone: '',
        academyId: '',
      },
      rules:{
        teacherId: [
          { required: true, message: '请输入教师编号', trigger: 'blur' },
          { min: 2, max: 8, message: '请输入2 - 8位数字!', trigger: 'blur' }
        ],
        teacherName: [
          { required: true, message: '请输入教师姓名', trigger: 'blur' },
          { min: 2, max: 20, message: '请输入2 - 20个字符!', trigger: 'blur' }
        ],
        teacherIdCard: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          { min: 18, max: 18, message: '请输入18位身份证号!', trigger: 'blur' }
        ],
        teacherPhone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入11位手机号!', trigger: 'blur' }
        ],
        academyId: [
          { required: true, message: '请选择学院', trigger: 'blur' },
        ],
      }
    }
  },
  created(){
    this.academyInit();
    this.getData();
  },
  methods:{
    submitForm(formName){ // 新增/修改教师
      this.$refs[formName].validate((valid) => {//校验格式
        if (valid) {//格式校验通过
          let path = `http://localhost:8081/teacher/updateTeacher/${this.ruleForm.oldTeacherId}`;
          this.$ajax.post(path,this.ruleForm).then(res=>{
            if(res.data.status === 0){
              this.$message.success(res.data.message);
              this.dialogForm = false;
            }
            else{
              this.$message.error(res.data.message);
            }
            this.getData();
          })
        }
      });
    },
    handleEdit(row){
      this.ruleForm.oldTeacherId = row.teacherId;
      this.ruleForm.teacherId = row.teacherId + '';
      this.ruleForm.teacherName = row.teacherName;
      this.ruleForm.teacherSex = row.teacherSex;
      this.ruleForm.teacherIdCard = row.teacherIdCard;
      this.ruleForm.teacherPhone = row.teacherPhone;
      this.ruleForm.academyId = row.academyId;
      this.dialogForm = true;
    },
    addTeacher(){
      this.ruleForm.oldTeacherId = -1;
      this.ruleForm.teacherId = '';
      this.ruleForm.teacherName = '';
      this.ruleForm.teacherSex = 3;
      this.ruleForm.teacherIdCard = '';
      this.ruleForm.teacherPhone = '';
      this.ruleForm.academyId = '';
      this.dialogForm = true;
    },
    initPwd(row){//重置密码
      this.$confirm('确认重置教师“' + row.teacherId + ' - '+ row.teacherName + '“的登录密码?').then(()=>{
        let path = 'http://localhost:8081/teacher/initTeacherPwd';
        this.$ajax.post(path,row).then(res=>{
          if(res.data.status === 0){
            this.$message.success(res.data.message);
          }
          else{
            this.$message.error('密码重置失败!');
          }
        })
      }).catch(() => {
        this.$message('已取消重置密码!');
      });
    },
    academyInit(){//初始化学院
      let path = 'http://localhost:8081/academy/selectAcademyCanUse';
      this.$ajax.post(path).then(res=>{
        this.academy = res.data.list
      })
    },
    handleCurrentChange(val){//翻页
      this.currPage = val;
      this.getData();
    },
    findTeacher(){
      this.currPage = 1;
      this.getData();
    },
    getData(){
      let path = `http://localhost:8081/teacher/getTeacher/${this.currPage}`;
      this.$ajax.post(path,this.form).then(res=>{
        this.pageInfo = res.data;
      })
    },
    handleClose(done){
      this.$confirm('确认关闭?').then(()=>{done();})
    }
  }
}
</script>

<style scoped>
span{
  font-size: 18px;
}
</style>